package com.mjl.shops.activity;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

import com.mjl.shops.R;
import com.mjl.shops.base.BaseActivity;
import com.mjl.shops.base.ShopApp;
import com.mjl.shops.bean.UserInfor;
import com.mjl.shops.utils.CommUtils;
import com.mjl.shops.utils.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class AccountLoginActivity extends BaseActivity {

    private String userId;

    @BindView(R.id.phone_code_et)
    EditText phone_code_et;

    @BindView(R.id.pass_code_et)
    EditText pass_code_et;

    @Override
    protected void setContentViewBefore() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    protected int getViews() {
        return R.layout.activity_account_login;
    }

    @Override
    protected void setViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void setDate() {
    }

    @Override
    protected String getStateColor() {
        return CommUtils.color_05000000;
    }

    @Override
    protected int getStetaBackgroundStyle() {
        return 1;
    }

    @OnClick({R.id.icon_back, R.id.icon_right_arrow,R.id.login_tv,R.id.into_registered_tv})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.icon_back:
                finish();
                break;
            case R.id.icon_right_arrow:
                break;
            case R.id.login_tv:
                String account = phone_code_et.getText().toString().trim();
                String pass = pass_code_et.getText().toString().trim();

                userId = StringUtils.setMD5(account);
                if (StringUtils.strEmpty(userId)) {
                    if (!StringUtils.strEmpty(account) || !StringUtils.isPhone(account)) {
                        CommUtils.setToast("手机号错误！");
                        return;
                    }

                    if (!StringUtils.strEmpty(pass)) {
                        CommUtils.setToast("密码错误！");
                        return;
                    }

                    BmobQuery<UserInfor> userQuery = new BmobQuery<>();
                    userQuery.addWhereEqualTo("user_id", userId);
                    userQuery.findObjects(new FindListener<UserInfor>() {
                        @Override
                        public void done(List<UserInfor> list, BmobException e) {
                            if (list != null && list.size() > 0) {
                                if (list.get(0).getPhone().equals(account)) {
                                    ShopApp.getSharedUtils().setString(CommUtils.UserId,userId);
                                    CommUtils.setToast("登录成功！");
                                    finish();
                                } else {
                                    CommUtils.setToast("账号错误！");
                                }
                            } else {
                                CommUtils.setToast("用户不存在！");
                            }
                        }
                    });
                } else {
                    CommUtils.setToast("用户不存在！");
                }
                break;
            case R.id.into_registered_tv:
                RegisteredActivity.StartIntent(AccountLoginActivity.this);
                finish();
                break;
            default:
                break;
        }
    }

    public static void StartIntent(Context contexts) {
        Intent intent = new Intent(contexts, AccountLoginActivity.class);
        contexts.startActivity(intent);
    }
}
